package flc.ast.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.more.about.DefAboutActivity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.SettingActivity;
import flc.ast.activity.WallpaperActivity;
import flc.ast.adapter.NoteAdapter;
import flc.ast.adapter.WallpaperAdapter;
import flc.ast.databinding.FragmentMyBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyFragment extends BaseNoModelFragment<FragmentMyBinding> {

    private NoteAdapter noteAdapter;
    private WallpaperAdapter wallpaperAdapter;
    private List<StkResBean> listStrategy = new ArrayList<>();
    private List<StkResBean> listWallpaper = new ArrayList<>();
    private int type=0;

    @Override
    protected int onCreate() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSetting.setOnClickListener(this);
        mDataBinding.llAgreement.setOnClickListener(this);
        mDataBinding.llFeedback.setOnClickListener(this);
        mDataBinding.llAbout.setOnClickListener(this);
        mDataBinding.llPrivacy.setOnClickListener(this);
        mDataBinding.rlStrategyCollect.setOnClickListener(this);
        mDataBinding.rlWallpaperCollect.setOnClickListener(this);
        mDataBinding.rvStrategyCollectList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        noteAdapter = new NoteAdapter();
        mDataBinding.rvStrategyCollectList.setAdapter(noteAdapter);
        noteAdapter.addChildClickViewIds(R.id.ivNoteItemGood);
        noteAdapter.setOnItemChildClickListener(this);
        noteAdapter.setOnItemClickListener(this);
        mDataBinding.rvWallpaperCollectList.setLayoutManager(new GridLayoutManager(mContext, 3));
        wallpaperAdapter = new WallpaperAdapter();
        mDataBinding.rvWallpaperCollectList.setAdapter(wallpaperAdapter);
        wallpaperAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlStrategyCollect://攻略收藏
                clearSelection();
                getStrategyData();
                type=0;
                mDataBinding.ivStrategyCollect.setImageResource(R.drawable.shape_collect_sel);
                mDataBinding.tvStrategyCollect.setTextColor(Color.parseColor("#000000"));
                mDataBinding.rvStrategyCollectList.setVisibility(View.VISIBLE);
                break;
            case R.id.rlWallpaperCollect://壁纸收藏
                clearSelection();
                getWallpaperData();
                type=1;
                mDataBinding.ivWallpaperCollect.setImageResource(R.drawable.shape_collect_sel);
                mDataBinding.tvWallpaperCollect.setTextColor(Color.parseColor("#000000"));
                mDataBinding.rvWallpaperCollectList.setVisibility(View.VISIBLE);
                break;
            case R.id.ivSetting://设置
                startActivity(SettingActivity.class);
                break;
            case R.id.llAgreement://用户协议
                BaseWebviewActivity.openAssetTerms(mContext);
                break;
            case R.id.llFeedback://意见反馈
                BaseWebviewActivity.openFeedback(mContext);
                break;
            case R.id.llAbout://关于我们
                startActivity(new Intent(getActivity(), DefAboutActivity.class));
                break;
            case R.id.llPrivacy://隐私政策
                BaseWebviewActivity.openAssetPrivacy(getActivity());
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter==noteAdapter){
            StkResBean bean = (StkResBean) adapter.getItem(position);
            BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
        }else if (adapter==wallpaperAdapter){
            WallpaperActivity.listWallpaper = (List<StkResBean>) adapter.getData();
            WallpaperActivity.selPosition=position;
            startActivity(WallpaperActivity.class);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivNoteItemGood:
                listStrategy.remove(noteAdapter.getItem(position));
                AppSpUtils.saveGoodList(listStrategy);
                getStrategyData();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){
            getData();
        }
    }

    public  void getData(){
        if (type==0){
            getStrategyData();
        }else if (type==1){
            getWallpaperData();
        }
    }

    private void getStrategyData() {
        listStrategy.clear();
        List<StkResBean> listSp = AppSpUtils.getGoodList();
        if (listSp != null && listSp.size() > 0) {
            for (StkResBean bean : listSp) {
                bean.setSelected(true);
                listStrategy.add(bean);
            }
            noteAdapter.setList(listStrategy);
            mDataBinding.rvStrategyCollectList.setVisibility(View.VISIBLE);
            mDataBinding.tvCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvStrategyCollectList.setVisibility(View.GONE);
            mDataBinding.tvCollectNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getWallpaperData() {
        listWallpaper.clear();
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            listWallpaper.addAll(listSp);
            wallpaperAdapter.setList(listWallpaper);
            mDataBinding.rvWallpaperCollectList.setVisibility(View.VISIBLE);
            mDataBinding.tvCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvWallpaperCollectList.setVisibility(View.GONE);
            mDataBinding.tvCollectNoData.setVisibility(View.VISIBLE);
        }
    }

    private void clearSelection() {
        mDataBinding.ivStrategyCollect.setImageResource(0);
        mDataBinding.tvStrategyCollect.setTextColor(Color.parseColor("#808080"));
        mDataBinding.rvStrategyCollectList.setVisibility(View.GONE);
        mDataBinding.ivWallpaperCollect.setImageResource(0);
        mDataBinding.tvWallpaperCollect.setTextColor(Color.parseColor("#808080"));
        mDataBinding.rvWallpaperCollectList.setVisibility(View.GONE);
    }

}